package com.north.light.androidutils.canvas.selectionprogressbar;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.north.light.androidutils.R;
import com.north.light.androidutils.log.LogUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: lzt
 * @CreateDate: 2021/7/30 10:30
 * @Version: 1.0
 * @Description:用于CoordinatorLayout，为了填充默认数据list的recyclerview
 */
public class SelectionDataRecyclerView extends RecyclerView {


    public SelectionDataRecyclerView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    }

    /**
     * 设置数据
     */
    public void initData() {
        CoorAdapter adapter = new CoorAdapter();
        List<String> da = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            da.add(i + "");
        }
        adapter.mData = da;
        this.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    public void setData() {

    }

    public static class CoorAdapter extends Adapter<CoorAdapter.CoorVH> {
        private List<String> mData = new ArrayList<>();

        @NotNull
        @Override
        public CoorVH onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
            return new CoorVH(LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.activity_canvas_adapter_item_txt,
                    parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull @NotNull CoorVH holder, int position) {
            String data = mData.get(position);
            holder.mContent.setText(data);
            //进度
            holder.progressView.setSelectionProgressBarListener(null);
            int rest = position % 3;
            if (rest == 0) {
                SelectionProgressBarInfo pInfo = new SelectionProgressBarInfo();
                pInfo.setStatus(0);
                holder.progressView.setProgress(pInfo, false);
            } else if (rest == 1) {
                SelectionProgressBarInfo pInfo = new SelectionProgressBarInfo();
                pInfo.setStatus(1);
                pInfo.setProgress(50);
                holder.progressView.setProgress(pInfo, false);
            } else if (rest == 2) {
                SelectionProgressBarInfo pInfo = new SelectionProgressBarInfo();
                pInfo.setStatus(2);
                pInfo.setProgress(30);
                holder.progressView.setProgress(pInfo, false);
                holder.progressView.setSelectionProgressBarListener(new SelectionProgressBarListener() {
                    @Override
                    public void progressChange(int progress, int max) {

                    }

                    @Override
                    public void click(View v, float x, float y) {
                        SelectionProgressBarInfo pInfo = new SelectionProgressBarInfo();
                        pInfo.setStatus(2);
                        pInfo.setProgress(20);
                        pInfo.setSelectTouchX(x);
                        pInfo.setSelectTouchY(y);
                        holder.progressView.setProgress(pInfo, true);
                        holder.lottieView.start("lp","lp/refresh.json");
                    }
                });
            }else {
                SelectionProgressBarInfo pInfo = new SelectionProgressBarInfo();
                pInfo.setStatus(0);
                holder.progressView.setProgress(pInfo, false);
            }
            holder.progressView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.d("progressView ");
                }
            });
//            holder.lottieView.start("lp","lp/refresh.json");

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public static class CoorVH extends ViewHolder {
            private TextView mContent;
            private SelectionProgressHorizontalBarView progressView;
            private SelectionLottieView lottieView;

            public CoorVH(@NonNull @NotNull View itemView) {
                super(itemView);
                mContent = itemView.findViewById(R.id.activity_coor_adapter_item_txt);
                progressView = itemView.findViewById(R.id.progressView);
                lottieView = itemView.findViewById(R.id.lottieView);
            }
        }
    }
}
